package com.patni.frame.util;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Area;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JWindow;

import com.patni.frame.util.edge.AidEdge;
import com.sun.awt.AWTUtilities;

import servbean.MyBounds;

import bean.FrameSetBean;

public class FrameFunction {
private JFrame mainPanel;
private FrameSet frameSet;
private FrameSetBean bean;

private FrameSet edge;
private FrameSetBean edgeBean;
private AidEdge aidEdge;
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
private int cursorType = -1;
private JWindow screen;

private boolean headflag;
private int tempX = 0;
private int tempY = 0;

private Area shape;

public void init(){

	headflag = false;
	
	mainPanel = new JFrame();
	mainPanel.setUndecorated(true);
	AWTUtilities.setWindowOpaque(mainPanel, false);
	mainPanel.setLayout(null);
	mainPanel.setBounds(0, 0, Common.screen_width, Common.screen_height);
   
//	edgePanel = new JWindow();
//	AWTUtilities.setWindowOpaque(edgePanel, false);
//	edgePanel.setLayout(null);
//	edgePanel.setBounds(0, 0, Common.screen_width, Common.screen_height);
	
	bean  = new FrameSetBean();
	boolean is = bean.init();

	frameSet = new FrameSet(bean);
	/**
	 * INIT edge frameSet bean
	 */
	edgeBean = new FrameSetBean();
	System.out.println(edgeBean.initEdge());
	edgeBean.getMyBounds().boundsUp(frameSet.getSetBean().getMyBounds(), 1);

	/**
	 * INIT aidEdge frameSet bean
	 */

	edge = new FrameSet(edgeBean);
	aidEdge = new AidEdge();
	
	/**
	 * 
	 */
	frameSet.init();
	edge.init();
	
	for(Component c : edge.getSetBean().getComponent()){
		mainPanel.add(c);
	}
	
	for(Component c : frameSet.getSetBean().getComponent()){
		mainPanel.add(c);
	}
	

	
	x = edge.getSetBean().getMyBounds().getX();
	y = edge.getSetBean().getMyBounds().getY();
	width = edge.getSetBean().getMyBounds().getWidth();
	height = edge.getSetBean().getMyBounds().getHeight();
	
	screen = new JWindow();
	AWTUtilities.setWindowOpacity(screen, 0.01f);
	screen.setBounds(0, 0, 1500, 1000);
	
	getImageShape();
	AWTUtilities.setWindowShape(mainPanel,shape);  
	
	addAdgeListener();
	
	addHeadListener();

	addSreenListener();
	
	addMontionListener();
}

private void addAdgeListener(){
	
	for(Component w : edge.getSetBean().getComponent()){

	w.addMouseListener(new MouseListener(){	

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub	
			screen.setAlwaysOnTop(true);
			screen.setVisible(true);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	});
	}
}

private void addSreenListener(){
	screen.addMouseListener(new MouseListener(){

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			aidEdge.show(x,y,width,height);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			screen.setAlwaysOnTop(false);
			screen.setVisible(false);
//			aidEdge.hide();
			reSize(aidEdge.getX(),aidEdge.getY(),aidEdge.getWidth(),aidEdge.getHeight());
		}
		
	});
	screen.addMouseMotionListener(new MouseMotionListener(){

		@Override
		public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
			

	
			int x = e.getX();
			int y = e.getY();
//			switch(cursorType){
//			case Cursor.N_RESIZE_CURSOR : 
//				if(y < y + height - 400){
//					aidEdge.setModle(x, y, width, y + height - y);
//				}
//				break;
//			case Cursor.E_RESIZE_CURSOR :
//				if(x > x + width - 250){
//				aidEdge.setModle(x, y, x - x,  height);
//				}
//				break;
//			case Cursor.S_RESIZE_CURSOR :
//				if(y > y + height - 400){
//					aidEdge.setModle(x, y, width, y - y );
//					}
//				break;
//			case Cursor.W_RESIZE_CURSOR :
//				if(x < x + width - 250){
//					aidEdge.setModle(x, y, x + width - x, height);
//				}
//				break;
//			case Cursor.NW_RESIZE_CURSOR : 
//				if(y < y + height - 400 && x < x + width - 250){
//					aidEdge.setModle(x, y, x + width - x, y + height - y);
//				}else if(y < y + height - 400 && x > x + width - 250){
//					aidEdge.setModle(x + width - 250, y, 250, y + height - y);
//				}else if(y > y + height - 400 && x < x + width - 250){
//					aidEdge.setModle(x, y + height - 400, x + width - x, 400);
//				}
//				break;
//			case Cursor.NE_RESIZE_CURSOR :
//				if(y < y + height - 400 && x > x + 250){
//					aidEdge.setModle(x, y, x - x, y + height - y);
//				}else if(y > y + height - 400 && x > x + 250){
//					aidEdge.setModle(x, y + height - 400, x - x, 400);
//				}else if(y < y + height - 400 && x < x + 250){
//					aidEdge.setModle(x, y, 250, y + height - y);
//				}
//				break;
//			case Cursor.SW_RESIZE_CURSOR :
//				if(y > y + 400 && x < x + width - 250){
//					aidEdge.setModle(x, y, x + width - x, y - y);
//				}else if(y > y + 400 && x > x + width - 250){
//					aidEdge.setModle(x + width - 250, y, 250, y - y);
//				}else if(y < y + 400 && x < x + width - 250){
//					aidEdge.setModle(x, y, x + width - x, 400);
//				}
//				break;
//			case Cursor.SE_RESIZE_CURSOR :
//				if(y > y + 400 && x > x + 250){
//					aidEdge.setModle(x, y, x - x, y - y);
//				}else if(y < y + 400 && x > x + 250){
//					aidEdge.setModle(x, y, x - x, 400);
//				}else if(y > y + 400 && x < x + 250){
//					aidEdge.setModle(x, y, 250, y - y);
//				}
//				break;
//			default : 
//				
//				aidEdge.setModle(x - tempX, y - tempY, width,height);
//				
//				break;
//			}			

			
			
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			
			// TODO Auto-generated method stub
			cursorType = Cursor.DEFAULT_CURSOR;
			headflag = false;
			
			int x  =e.getX();
			int y = e.getY();
						
			if(x > x - 2 && x < x + 2){
				if(y <= y + 2 && y >= y){
					cursorType = Cursor.NW_RESIZE_CURSOR;
				}else if(y > y + 2 && y < y + height - 2){
					cursorType = Cursor.W_RESIZE_CURSOR;
				} else if(y >= y + height - 2 && y <= y + height){
					cursorType = Cursor.SW_RESIZE_CURSOR;
				}
			}else if(x > x + width - 3 && x < x + width + 1){
				if(y <= y + 2 && y >= y){
					cursorType = Cursor.NE_RESIZE_CURSOR;
				}else if(y > y + 2 && y < y + height - 2){
					cursorType = Cursor.E_RESIZE_CURSOR;
				} else if(y >= y + height - 2 && y <= y + height){
					cursorType = Cursor.SE_RESIZE_CURSOR;
				}
			}else if(y > y -2 && y < y + 2){
				if(x <= x + 2 && x >= x){
					cursorType = Cursor.NW_RESIZE_CURSOR;
				}else if(x > x + 2 && x < x + width - 2){
					cursorType = Cursor.N_RESIZE_CURSOR;
				} else if(x >= x + width - 2 && x <= x + width){
					cursorType = Cursor.NE_RESIZE_CURSOR;
				}
			}else if(y > y + height - 3 && y < y + height + 1){
				if(x <= x + 2 && x >= x){
					cursorType = Cursor.SW_RESIZE_CURSOR;
				}else if(x > x + 2 && x < x + width - 2){
					cursorType = Cursor.S_RESIZE_CURSOR;
				} else if(x >= x + width - 2 && x <= x + width){
					cursorType = Cursor.SE_RESIZE_CURSOR;
				}
			}
			else if(x > x && x < x + width - 3*Common.header_button_width && 
					y > y && y < y + Common.header_height ){
				headflag = true;
				tempX = x - x;
				tempY = y - y;
			}
			
			if(cursorType != Cursor.DEFAULT_CURSOR || headflag){
				screen.setCursor(new Cursor(cursorType));
			}else{
				screen.setAlwaysOnTop(false);
				screen.setVisible(false);
			}
			
		}
	});
}

private void addHeadListener(){
	mainPanel.addMouseMotionListener(new MouseMotionListener(){

		@Override
		public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			
			int x = e.getX();
			int y = e.getY();

			if(x > x && x < x + width - 3*Common.header_button_width && 
					y > y && y < y + Common.header_height ){
				screen.setAlwaysOnTop(true);
				screen.setVisible(true);
			}
		}
		
	});
	
	mainPanel.addMouseListener(new MouseListener(){

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			int x = e.getX();
			int y = e.getY();
			if(x > x  + width - 1*Common.header_button_width && x < x + width  && 
					y > y && y < y + Common.header_button_height ){
//				hide();
				System.out.println("close !!!!!!");
			}else if(x > x  + width - 2*Common.header_button_width && x < x + width - 1*Common.header_button_width && 
					y > y && y < y + Common.header_button_height ){
				System.out.println("minSize or maxSize !");
			}else if(x > x  + width - 3*Common.header_button_width && x < x + width - 2*Common.header_button_width && 
					y > y && y < y + Common.header_button_height ){
				System.out.println("---zui xiao hua ! --");
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	});
}

private void addMontionListener(){
	mainPanel.addFocusListener( new FocusListener(){

		@Override
		public void focusGained(FocusEvent e) {
			// TODO Auto-generated method stub
//			mainPanel.setAlwaysOnTop(true);
//			edgePanel.setAlwaysOnTop(true);
		}

		@Override
		public void focusLost(FocusEvent e) {
			// TODO Auto-generated method stub
			System.out.println("focusLost");
//			mainPanel.toBack();
			System.out.println(mainPanel.getX()+"   "+ mainPanel.getY()+"  "+ mainPanel.getWidth());
		
		}
		
	});
	
	mainPanel.addComponentListener(new ComponentListener(){

		@Override
		public void componentHidden(ComponentEvent e) {
			// TODO Auto-generated method stub
			System.out.println("componentHidden");
		}

		@Override
		public void componentMoved(ComponentEvent e) {
			// TODO Auto-generated method stub
			System.out.println("componentMoved");
		}

		@Override
		public void componentResized(ComponentEvent e) {
			// TODO Auto-generated method stub
			System.out.println("componentResized");
			System.out.println(mainPanel.getWidth());
		}

		@Override
		public void componentShown(ComponentEvent e) {
			// TODO Auto-generated method stub
			System.out.println("componentShown");
		}
		
	});
}

private void reSize(int x , int y, int width, int height){
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	edgeBean.getMyBounds().setX(x);
	edgeBean.getMyBounds().setY(y);
	edgeBean.getMyBounds().setWidth(width);
	edgeBean.getMyBounds().setHeight(height);
	bean.getMyBounds().boundsDown(edgeBean.getMyBounds(), 1);
	frameSet.init();
	edge.init();
	
	getImageShape();
	AWTUtilities.setWindowShape(mainPanel,shape);  
}

private void	getImageShape(){
	shape=new Area();
	
	for(int i = 4;i>=1;i--){
		shape.add(new Area(new Rectangle(x + i,y + 4-i,width-2*i,1)));
	}
	shape.add(new Area(new Rectangle(x,y + 4,width,height-4*2)));
	
	for(int i=1;i<=4;i++){
	shape.add(new Area(new Rectangle(x + i,y + height-5+i,width-2*i,1)));
	}
	
}

public void show(){
//	edgePanel.setVisible(true);
	mainPanel.setVisible(true);
}
public  void minSize(){
	mainPanel.setLocation(-2*Common.screen_width, -2*Common.screen_height);
//	edgePanel.setLocation(-2*Common.screen_width, -2*Common.screen_height);
}

public void refresh(){
	mainPanel.repaint();
//	edgePanel.repaint();
}

public  void hide(){
	mainPanel.setVisible(false);
//	edgePanel.setVisible(false);
}

public FrameSet getFrameSet() {
	return frameSet;
}

public void setFrameSet(FrameSet frameSet) {
	
	this.frameSet = frameSet;
}
public FrameSet getEdge() {
	return edge;
}
public void setEdge(FrameSet edge) {
	this.edge = edge;
}

public AidEdge getAidEdge() {
	return aidEdge;
}

public void setAidEdge(AidEdge aidEdge) {
	this.aidEdge = aidEdge;
}



public int getWidth() {
	return width;
}

public void setWidth(int width) {
	this.width = width;
}

public int getHeight() {
	return height;
}

public void setHeight(int height) {
	this.height = height;
}

public int getCursorType() {
	return cursorType;
}

public void setCursorType(int cursorType) {
	this.cursorType = cursorType;
}

public JWindow getScreen() {
	return screen;
}

public void setScreen(JWindow screen) {
	this.screen = screen;
}

public int getX() {
	return x;
}

public void setX(int x) {
	this.x = x;
}

public int getY() {
	return y;
}

public void setY(int y) {
	this.y = y;
}


}
